const isDev = process.env.NODE_ENV !== 'production';

module.exports = function babelConfig(api) {
    api.cache(true);

    const presets = [
        ['@babel/preset-env', { targets: { browsers: ['safari >= 7', 'ie >= 9'] } }],
        '@babel/preset-react',
    ];
    const plugins = [
        'babel-plugin-macros',
        'babel-plugin-transform-prune-unused-imports',
        'babel-plugin-minify-dead-code-elimination',
        '@babel/plugin-proposal-nullish-coalescing-operator',
        '@babel/plugin-proposal-optional-chaining',
        '@babel/plugin-syntax-dynamic-import',
        'webpack-chunkname',
        '@babel/plugin-proposal-object-rest-spread',
        ['@babel/plugin-proposal-decorators', { legacy: true }],
        ['@babel/plugin-proposal-class-properties', { loose: true }],
        isDev && '@babel/plugin-transform-react-display-name',
        ["import", {libraryName: "antd-mobile", style: "css"}]
    ].filter(Boolean);
    if (process.env.NODE_TEST === 'jest') {
        plugins.push([
            '@babel/plugin-transform-runtime',
            {
                helpers: false, // 定义了一些处理新的语法关键字的帮助函数
            },
        ]);
        plugins.push('@babel/plugin-transform-modules-commonjs');
    }

    return {
        presets,
        plugins,
    };
};
